package datastructure.stack;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author XY
 * @version 1.0
 * @date 2022/2/22 9:11
 * @Description
 */
public class Leet255 {

    static class MyStack {

        Queue<Integer> queue = new ArrayDeque<>();
        Queue<Integer> queue1 = new ArrayDeque<>();

        public MyStack() {

        }

        public void push(int x) {
            queue.add(x);
        }

        public int pop() {
            Integer poll = null;
            if(queue.size()==0){
                if(queue1.size()>1){
                    int size = queue1.size();
                    for (int i = 0; i < size -1; i++) {
                        queue.add(queue1.poll());
                    }
                }
                poll = queue1.poll();
            }else{
                if (queue.size()>1) {
                    int size = queue.size();
                    for (int i = 0; i < size -1; i++) {
                        queue1.add(queue.poll());
                    }
                }
                poll = queue.poll();
            }
            return poll;
        }

        public int top() {
            Integer poll = null;
            if(queue.size()==0){
                if(queue1.size()>1){
                    int size = queue1.size();
                    for (int i = 0; i < size -1; i++) {
                        queue.add(queue1.poll());
                    }
                }
                poll = queue1.poll();
                queue.add(poll);

            }else{
                if (queue.size()>1) {
                    int size = queue.size();
                    for (int i = 0; i < size -1; i++) {
                        queue1.add(queue.poll());
                    }
                }
                poll = queue.poll();
                queue1.add(poll);
            }
            return poll;
        }

        public boolean empty() {
            return queue.isEmpty()&&queue1.isEmpty();
        }
    }

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        System.out.println(myStack.top());
        System.out.println(myStack.pop());
        System.out.println(myStack.top());

//        System.out.println(myStack.pop());
//        System.out.println(myStack.pop());
        System.out.println(myStack.empty());
    }

}
